home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Rewrite 0.2.6 / ReWrite 0.2.6 Docs / ReWrite 0.2.6 Docs.rsrc / TEXT_133.txt < prev    next >
Encoding:
Text File  |  1995-08-23  |  6.4 KB  |  218 lines

  1.  
  2. Chapter 5 - Lists, Eq and Output
  3.  
  4. This section gives the ReWrite functions that operate on lists, functions that deal with equality, and functions that send output to the screen. list, splice and outnum are particularly important.
  5.  
  6. List Operations
  7.  
  8. list, splice
  9.  
  10. list and splice are the two most important functions in ReWrite.
  11.  
  12. list makes a list out of whatever it is passed.
  13.  
  14. splice is the inverse of list: it takes a list as an argument, and returns the entries as results. splice is a good example of a function than returns a variable number of arguments.
  15.  
  16. list[vals ]  or  {vals } -> list ;
  17. (directly coded)
  18.  
  19. splice[list ]  or  .list  -> vals ;
  20. (directly coded)
  21.  
  22. Example:
  23.   splice[{1,{2,3},4}] -> 1,{2,3},4
  24.  
  25.   list[1,{2,3},4] -> {1,{2,3},4}
  26.  
  27. Note:
  28.   splice[list[x ]]= x , but 
  29.   list[splice[x ]] may not be identical to x , as splice[x ] requires x  to be a list.
  30.  
  31. join
  32.  
  33. join combines several lists into one.
  34.  
  35. join[lists ]-> list ;
  36.  
  37.   join[] -> {};
  38.   join[a:lis] -> a;
  39.   join[a:lis,.rest] -> join[{.a,.b},.rest];
  40.  
  41. Note that the first line of this definition is all that would be necessary to join two lists, the other lines merely extend this.
  42.  
  43. Example:
  44.   join[{1,2,3},{},{4},{5,6}] -> {1,2,3,4,5,6}
  45.  
  46. nth, nthcdr
  47.  
  48. nth is a function that returns the nth element of a list.
  49.  
  50. nth[int ,list ]-> val ;
  51.  
  52.   nth[1,{a,.b}] -> a;
  53.   nth[x:int,{a,.b}] -> nth[x-1,b];
  54.  
  55. nthcdr is a function that returns the list with the first n elements removed.
  56.  
  57. nthcdr[int ,list ]-> list ;
  58.  
  59.   nthcdr[0,a] -> a;
  60.   nthcdr[x:int,{a,.b}] -> nthcdr[x-1,b];
  61.   nthcdr[x:int,{}] -> {};
  62.  
  63. Note that lists are indexed from 1 being the first element, so for example:
  64.   nth[3,{41,42,43,44}] -> 43
  65.   nthcdr[3,{41,42,43,44}] -> {43,44}
  66.  
  67. length
  68.  
  69. length returns the number of elements in a list passed to it.
  70.  
  71. length[list ]-> int ;
  72.  
  73.   length[{}] -> 0;
  74.   length[{a,.b}] -> length[b]+1;
  75.  
  76. Example:
  77.   length[{41,42,{4,3,9},44}] -> 4
  78.  
  79. ismember
  80.  
  81. Given a value and a list, ismember returns true if the value is contained in the list, false otherwise.
  82.  
  83. ismember[val ,list ]-> bool ;
  84.  
  85.   ismember[x,{._,x,._}] -> true;
  86.   ismember[x,y:lis] -> false;
  87.  
  88. Example:
  89.   ismember[4,{1,2,3,5}] -> false
  90.   ismember[3,{1,2,3,5}] -> true
  91.  
  92. nil, prepend, append, car, cdr
  93.  
  94. These list constructor functions are somewhat superfluous, as the same results can usually be achieved more efficiently with list and splice. They are here only for completeness.
  95.  
  96. nil returns an empty list.
  97. nil[] -> {};
  98.  
  99.   nil[] -> {};
  100.  
  101. prepend prepends a value to the start of a list.
  102. prepend[val ,list ]-> list ;
  103.  
  104.   prepend[x,l:lis] -> {x,.l};
  105.  
  106. append appends a value to the end of a list.
  107. append[val ,list ]-> list ;
  108.  
  109.   append[x,l:lis] -> {.l,x};
  110.  
  111. car returns the first element of a list.
  112. car[list ]-> val ;
  113.  
  114.   car[{a,.rest}] -> a;
  115.  
  116. cdr returns a list with the first element removed.
  117. cdr[list ]-> list ;
  118.  
  119.   cdr[{a,.rest}] -> rest;
  120.  
  121. Examples:
  122.   prepend[1,{2,3,4}]  ->  {1,2,3,4}
  123.    This is the same as {1,.{2,3,4}} -> {1,2,3,4}
  124.  
  125.   append[1,{2,3,4}] -> {2,3,4,1}
  126.  
  127. Equality Operations
  128.  
  129. These two functions compare two arguments two see whether they are equal or not. The two arguments can be anything. These should not need to be used much, since usually any checking they do can be done much easier as part of a match.
  130.  
  131. eq[val ,val ]  or  val = val -> bool ;
  132.  
  133.   eq[x,x] -> true;
  134.   eq[x,y] -> false;
  135.  
  136. ne[val ,val ]  or  val != val -> bool ;
  137.  
  138.   ne[x,x] -> true;
  139.   ne[x,y] -> false;
  140.  
  141. Screen Output
  142.  
  143. These are the functions that output to the screen. None of them are directly coded, but the full coding is not very illuminating for some or them, so has been omitted.
  144.  
  145. outnum, outbase, out, outln
  146.  
  147. outnum outputs its arguments to screen in the same form that they could be input, assuming that all the numbers are to be displayed base 10. outbase does the same as outnum, except you specify the base. out and outln are similar to outnum, but suppress the quotes on characters and tokens. Note that a separate function for displaying lists is not required, as all four will display the list structure.
  148.  
  149. outnum[vals ] -> ;
  150. outbase[int ,vals ] -> ;
  151. out[vals ] -> ;
  152. outln[vals ] -> ;
  153.  
  154.   outnum[.x] -> outbase[10,.x];
  155.  
  156. For example if there was a rule:
  157.  
  158.   outall[.x] -> outnum[.x],outbase[2,.x],out[.x],outln[.x];
  159.  
  160.   outall[{{$41,{66}},{},`token,"string",false,null}]
  161. would return nothing and print to the screen the following:
  162.  
  163. {{65,{66}},{},`token,"string",false,null}
  164. {{1000001,{1000010}},{},`token,"string",false,null}
  165. {{65{66}}{}tokenstringfalsenull}{{65{66}}{}tokenstringfalsenull}
  166.  
  167. Note that the result of outnum could be copied and pasted into a ReWrite program, the other three could not.
  168.  
  169. There are some other 'out' functions - outs and outstring. These should not be used, as they are only there for compatibility with some old code in the compiler.
  170.  
  171. prettyprint, prettyprint16
  172.  
  173. These are routines that are similar to outnum and outbase with a base of 16, the difference being that the attempt to break up long structures into manageable sized portions on each line so that the structure can be seen more easily. Note that they can also be suppressed by holding down the mouse button (useful if the list was much longer than expected, but you don't want to abort).
  174.  
  175. prettyprint[vals ] -> ;
  176. prettyprint16[vals ] -> ;
  177.  
  178. SYSresidue
  179.  
  180. This is a function that tells ReWrite how to deal with any 'extra stuff' left over at the end of an evaluation. This can be overwritten by the user. It is important that SYSresidue returns nothing. The default definition is the following:
  181.  
  182.   SYSresidue[] -> ;
  183.   SYSresidue[.x] -> outs['Returned:'],prettyprint[.x];
  184.  
  185. For example:
  186.  
  187.   top[] -> outnum[29],71;
  188.  
  189. returns:
  190.  
  191. 29
  192. Returned:
  193. 71
  194.  
  195. outand (a note on debugging)
  196.  
  197. outand is an example of a very simple diagnostic routine. If you put it around part or all of any expressions on the right hand side of a rule it will display the result, but otherwise be transparent. Also, since the other output functions return nothing, they can also be transparently inserted into the rhs of expressions.
  198.  
  199. outand[vals ] -> vals ;
  200.  
  201.   outand[.x] -> prettyprint[.x],.x;
  202.  
  203. For example we could change this:
  204.   thecall[x,{y,.z}] -> buggytrickyfunction[x,y,z];
  205.  
  206. into this:
  207.   thecall[x,{y,.z}] -> outstring['#tricky'],
  208.         outand[buggytrickyfunction[outand[x,y,z]]];
  209.  
  210. This would output:
  211.  
  212. #tricky
  213. the arguments passed to buggytrickyfunction
  214. the result returned from buggytrickyfunction
  215.  
  216. all transparent to the code. (OK, it's not any sort of replacement for a full symbolic debugger with stack dump, trace and single step, but it's easier to write and will have to do for the moment).
  217.  
  218.